using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace CDTM.Modules.TraCuuDiem.Components
{
	public partial class RenLuyen
	{
		#region Properties.
		
		public string MaSV { set; get; }
		public string HoTen { set; get; }
		public string Lop { set; get; }
		public DateTime NgaySinh { set; get; }
		public string XepLoaiHT { set; get; }
		public int YTHT_1 { set; get; }
		public int YTHT_2 { set; get; }
		public int YTHT_3 { set; get; }
		public int CHNQ_1 { set; get; }
		public int CHNQ_2 { set; get; }
		public int CHNQ_3 { set; get; }
		public int CHNQ_4 { set; get; }
		public int TGHDXH { set; get; }
		public int PCCD_1 { set; get; }
		public int PCCD_2 { set; get; }
		public int PTLDT { set; get; }
		public int DiemTK { set; get; }
		public string XepLoai { set; get; }
		public string GhiChu { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static RenLuyen Load(string maSV)
		{
			const string spName = "[dbo].[p_CDTM_TCD_RenLuyen_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, maSV);
			RenLuyen entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new RenLuyen();
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTen"))) entity.HoTen = reader.GetString(reader.GetOrdinal("HoTen"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoaiHT"))) entity.XepLoaiHT = reader.GetString(reader.GetOrdinal("XepLoaiHT"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_1"))) entity.YTHT_1 = reader.GetInt32(reader.GetOrdinal("YTHT_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_2"))) entity.YTHT_2 = reader.GetInt32(reader.GetOrdinal("YTHT_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_3"))) entity.YTHT_3 = reader.GetInt32(reader.GetOrdinal("YTHT_3"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_1"))) entity.CHNQ_1 = reader.GetInt32(reader.GetOrdinal("CHNQ_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_2"))) entity.CHNQ_2 = reader.GetInt32(reader.GetOrdinal("CHNQ_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_3"))) entity.CHNQ_3 = reader.GetInt32(reader.GetOrdinal("CHNQ_3"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_4"))) entity.CHNQ_4 = reader.GetInt32(reader.GetOrdinal("CHNQ_4"));
				if (!reader.IsDBNull(reader.GetOrdinal("TGHDXH"))) entity.TGHDXH = reader.GetInt32(reader.GetOrdinal("TGHDXH"));
				if (!reader.IsDBNull(reader.GetOrdinal("PCCD_1"))) entity.PCCD_1 = reader.GetInt32(reader.GetOrdinal("PCCD_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("PCCD_2"))) entity.PCCD_2 = reader.GetInt32(reader.GetOrdinal("PCCD_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("PTLDT"))) entity.PTLDT = reader.GetInt32(reader.GetOrdinal("PTLDT"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTK"))) entity.DiemTK = reader.GetInt32(reader.GetOrdinal("DiemTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoai"))) entity.XepLoai = reader.GetString(reader.GetOrdinal("XepLoai"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
			}
			reader.Close();
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<RenLuyen> SelectCollectionAll()
		{
			List<RenLuyen> collection = new List<RenLuyen>();
			SqlDataReader reader = (SqlDataReader) SelectReaderAll();
			while (reader.Read())
			{
				RenLuyen entity = new RenLuyen();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTen"))) entity.HoTen = reader.GetString(reader.GetOrdinal("HoTen"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoaiHT"))) entity.XepLoaiHT = reader.GetString(reader.GetOrdinal("XepLoaiHT"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_1"))) entity.YTHT_1 = reader.GetInt32(reader.GetOrdinal("YTHT_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_2"))) entity.YTHT_2 = reader.GetInt32(reader.GetOrdinal("YTHT_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_3"))) entity.YTHT_3 = reader.GetInt32(reader.GetOrdinal("YTHT_3"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_1"))) entity.CHNQ_1 = reader.GetInt32(reader.GetOrdinal("CHNQ_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_2"))) entity.CHNQ_2 = reader.GetInt32(reader.GetOrdinal("CHNQ_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_3"))) entity.CHNQ_3 = reader.GetInt32(reader.GetOrdinal("CHNQ_3"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_4"))) entity.CHNQ_4 = reader.GetInt32(reader.GetOrdinal("CHNQ_4"));
				if (!reader.IsDBNull(reader.GetOrdinal("TGHDXH"))) entity.TGHDXH = reader.GetInt32(reader.GetOrdinal("TGHDXH"));
				if (!reader.IsDBNull(reader.GetOrdinal("PCCD_1"))) entity.PCCD_1 = reader.GetInt32(reader.GetOrdinal("PCCD_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("PCCD_2"))) entity.PCCD_2 = reader.GetInt32(reader.GetOrdinal("PCCD_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("PTLDT"))) entity.PTLDT = reader.GetInt32(reader.GetOrdinal("PTLDT"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTK"))) entity.DiemTK = reader.GetInt32(reader.GetOrdinal("DiemTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoai"))) entity.XepLoai = reader.GetString(reader.GetOrdinal("XepLoai"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<RenLuyen> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			List<RenLuyen> collection = new List<RenLuyen>();

			SqlDataReader reader = (SqlDataReader) SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				RenLuyen entity = new RenLuyen();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTen"))) entity.HoTen = reader.GetString(reader.GetOrdinal("HoTen"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoaiHT"))) entity.XepLoaiHT = reader.GetString(reader.GetOrdinal("XepLoaiHT"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_1"))) entity.YTHT_1 = reader.GetInt32(reader.GetOrdinal("YTHT_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_2"))) entity.YTHT_2 = reader.GetInt32(reader.GetOrdinal("YTHT_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("YTHT_3"))) entity.YTHT_3 = reader.GetInt32(reader.GetOrdinal("YTHT_3"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_1"))) entity.CHNQ_1 = reader.GetInt32(reader.GetOrdinal("CHNQ_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_2"))) entity.CHNQ_2 = reader.GetInt32(reader.GetOrdinal("CHNQ_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_3"))) entity.CHNQ_3 = reader.GetInt32(reader.GetOrdinal("CHNQ_3"));
				if (!reader.IsDBNull(reader.GetOrdinal("CHNQ_4"))) entity.CHNQ_4 = reader.GetInt32(reader.GetOrdinal("CHNQ_4"));
				if (!reader.IsDBNull(reader.GetOrdinal("TGHDXH"))) entity.TGHDXH = reader.GetInt32(reader.GetOrdinal("TGHDXH"));
				if (!reader.IsDBNull(reader.GetOrdinal("PCCD_1"))) entity.PCCD_1 = reader.GetInt32(reader.GetOrdinal("PCCD_1"));
				if (!reader.IsDBNull(reader.GetOrdinal("PCCD_2"))) entity.PCCD_2 = reader.GetInt32(reader.GetOrdinal("PCCD_2"));
				if (!reader.IsDBNull(reader.GetOrdinal("PTLDT"))) entity.PTLDT = reader.GetInt32(reader.GetOrdinal("PTLDT"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTK"))) entity.DiemTK = reader.GetInt32(reader.GetOrdinal("DiemTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("XepLoai"))) entity.XepLoai = reader.GetString(reader.GetOrdinal("XepLoai"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_CDTM_TCD_RenLuyen_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_TCD_RenLuyen_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_CDTM_TCD_RenLuyen_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_TCD_RenLuyen_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertRenLuyen(string hoTen, string lop, DateTime ngaySinh, string xepLoaiHT, int yTHT_1, int yTHT_2, int yTHT_3, int cHNQ_1, int cHNQ_2, int cHNQ_3, int cHNQ_4, int tGHDXH, int pCCD_1, int pCCD_2, int pTLDT, int diemTK, string xepLoai, string ghiChu)
		{
			RenLuyen entity = new RenLuyen();	
			entity.HoTen = hoTen;
			entity.Lop = lop;
			entity.NgaySinh = ngaySinh;
			entity.XepLoaiHT = xepLoaiHT;
			entity.YTHT_1 = yTHT_1;
			entity.YTHT_2 = yTHT_2;
			entity.YTHT_3 = yTHT_3;
			entity.CHNQ_1 = cHNQ_1;
			entity.CHNQ_2 = cHNQ_2;
			entity.CHNQ_3 = cHNQ_3;
			entity.CHNQ_4 = cHNQ_4;
			entity.TGHDXH = tGHDXH;
			entity.PCCD_1 = pCCD_1;
			entity.PCCD_2 = pCCD_2;
			entity.PTLDT = pTLDT;
			entity.DiemTK = diemTK;
			entity.XepLoai = xepLoai;
			entity.GhiChu = ghiChu;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_CDTM_TCD_RenLuyen_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@HoTen", SqlDbType.NVarChar, HoTen);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year < 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@XepLoaiHT", SqlDbType.NVarChar, XepLoaiHT);
			db.AddInParameter(dbCommand, "@YTHT_1", SqlDbType.Int, YTHT_1);
			db.AddInParameter(dbCommand, "@YTHT_2", SqlDbType.Int, YTHT_2);
			db.AddInParameter(dbCommand, "@YTHT_3", SqlDbType.Int, YTHT_3);
			db.AddInParameter(dbCommand, "@CHNQ_1", SqlDbType.Int, CHNQ_1);
			db.AddInParameter(dbCommand, "@CHNQ_2", SqlDbType.Int, CHNQ_2);
			db.AddInParameter(dbCommand, "@CHNQ_3", SqlDbType.Int, CHNQ_3);
			db.AddInParameter(dbCommand, "@CHNQ_4", SqlDbType.Int, CHNQ_4);
			db.AddInParameter(dbCommand, "@TGHDXH", SqlDbType.Int, TGHDXH);
			db.AddInParameter(dbCommand, "@PCCD_1", SqlDbType.Int, PCCD_1);
			db.AddInParameter(dbCommand, "@PCCD_2", SqlDbType.Int, PCCD_2);
			db.AddInParameter(dbCommand, "@PTLDT", SqlDbType.Int, PTLDT);
			db.AddInParameter(dbCommand, "@DiemTK", SqlDbType.Int, DiemTK);
			db.AddInParameter(dbCommand, "@XepLoai", SqlDbType.NVarChar, XepLoai);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
			{
				return db.ExecuteNonQuery(dbCommand, transaction);
			}
            else
			{
				return db.ExecuteNonQuery(dbCommand);
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<RenLuyen> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (RenLuyen item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateRenLuyen(string maSV, string hoTen, string lop, DateTime ngaySinh, string xepLoaiHT, int yTHT_1, int yTHT_2, int yTHT_3, int cHNQ_1, int cHNQ_2, int cHNQ_3, int cHNQ_4, int tGHDXH, int pCCD_1, int pCCD_2, int pTLDT, int diemTK, string xepLoai, string ghiChu)
		{
			RenLuyen entity = new RenLuyen();			
			entity.MaSV = maSV;
			entity.HoTen = hoTen;
			entity.Lop = lop;
			entity.NgaySinh = ngaySinh;
			entity.XepLoaiHT = xepLoaiHT;
			entity.YTHT_1 = yTHT_1;
			entity.YTHT_2 = yTHT_2;
			entity.YTHT_3 = yTHT_3;
			entity.CHNQ_1 = cHNQ_1;
			entity.CHNQ_2 = cHNQ_2;
			entity.CHNQ_3 = cHNQ_3;
			entity.CHNQ_4 = cHNQ_4;
			entity.TGHDXH = tGHDXH;
			entity.PCCD_1 = pCCD_1;
			entity.PCCD_2 = pCCD_2;
			entity.PTLDT = pTLDT;
			entity.DiemTK = diemTK;
			entity.XepLoai = xepLoai;
			entity.GhiChu = ghiChu;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_CDTM_TCD_RenLuyen_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@HoTen", SqlDbType.NVarChar, HoTen);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year == 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@XepLoaiHT", SqlDbType.NVarChar, XepLoaiHT);
			db.AddInParameter(dbCommand, "@YTHT_1", SqlDbType.Int, YTHT_1);
			db.AddInParameter(dbCommand, "@YTHT_2", SqlDbType.Int, YTHT_2);
			db.AddInParameter(dbCommand, "@YTHT_3", SqlDbType.Int, YTHT_3);
			db.AddInParameter(dbCommand, "@CHNQ_1", SqlDbType.Int, CHNQ_1);
			db.AddInParameter(dbCommand, "@CHNQ_2", SqlDbType.Int, CHNQ_2);
			db.AddInParameter(dbCommand, "@CHNQ_3", SqlDbType.Int, CHNQ_3);
			db.AddInParameter(dbCommand, "@CHNQ_4", SqlDbType.Int, CHNQ_4);
			db.AddInParameter(dbCommand, "@TGHDXH", SqlDbType.Int, TGHDXH);
			db.AddInParameter(dbCommand, "@PCCD_1", SqlDbType.Int, PCCD_1);
			db.AddInParameter(dbCommand, "@PCCD_2", SqlDbType.Int, PCCD_2);
			db.AddInParameter(dbCommand, "@PTLDT", SqlDbType.Int, PTLDT);
			db.AddInParameter(dbCommand, "@DiemTK", SqlDbType.Int, DiemTK);
			db.AddInParameter(dbCommand, "@XepLoai", SqlDbType.NVarChar, XepLoai);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<RenLuyen> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (RenLuyen item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateRenLuyen(string maSV, string hoTen, string lop, DateTime ngaySinh, string xepLoaiHT, int yTHT_1, int yTHT_2, int yTHT_3, int cHNQ_1, int cHNQ_2, int cHNQ_3, int cHNQ_4, int tGHDXH, int pCCD_1, int pCCD_2, int pTLDT, int diemTK, string xepLoai, string ghiChu)
		{
			RenLuyen entity = new RenLuyen();			
			entity.MaSV = maSV;
			entity.HoTen = hoTen;
			entity.Lop = lop;
			entity.NgaySinh = ngaySinh;
			entity.XepLoaiHT = xepLoaiHT;
			entity.YTHT_1 = yTHT_1;
			entity.YTHT_2 = yTHT_2;
			entity.YTHT_3 = yTHT_3;
			entity.CHNQ_1 = cHNQ_1;
			entity.CHNQ_2 = cHNQ_2;
			entity.CHNQ_3 = cHNQ_3;
			entity.CHNQ_4 = cHNQ_4;
			entity.TGHDXH = tGHDXH;
			entity.PCCD_1 = pCCD_1;
			entity.PCCD_2 = pCCD_2;
			entity.PTLDT = pTLDT;
			entity.DiemTK = diemTK;
			entity.XepLoai = xepLoai;
			entity.GhiChu = ghiChu;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_TCD_RenLuyen_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@HoTen", SqlDbType.NVarChar, HoTen);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year == 1753 ? DBNull.Value : (object) NgaySinh);
			db.AddInParameter(dbCommand, "@XepLoaiHT", SqlDbType.NVarChar, XepLoaiHT);
			db.AddInParameter(dbCommand, "@YTHT_1", SqlDbType.Int, YTHT_1);
			db.AddInParameter(dbCommand, "@YTHT_2", SqlDbType.Int, YTHT_2);
			db.AddInParameter(dbCommand, "@YTHT_3", SqlDbType.Int, YTHT_3);
			db.AddInParameter(dbCommand, "@CHNQ_1", SqlDbType.Int, CHNQ_1);
			db.AddInParameter(dbCommand, "@CHNQ_2", SqlDbType.Int, CHNQ_2);
			db.AddInParameter(dbCommand, "@CHNQ_3", SqlDbType.Int, CHNQ_3);
			db.AddInParameter(dbCommand, "@CHNQ_4", SqlDbType.Int, CHNQ_4);
			db.AddInParameter(dbCommand, "@TGHDXH", SqlDbType.Int, TGHDXH);
			db.AddInParameter(dbCommand, "@PCCD_1", SqlDbType.Int, PCCD_1);
			db.AddInParameter(dbCommand, "@PCCD_2", SqlDbType.Int, PCCD_2);
			db.AddInParameter(dbCommand, "@PTLDT", SqlDbType.Int, PTLDT);
			db.AddInParameter(dbCommand, "@DiemTK", SqlDbType.Int, DiemTK);
			db.AddInParameter(dbCommand, "@XepLoai", SqlDbType.NVarChar, XepLoai);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<RenLuyen> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (RenLuyen item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteRenLuyen(string maSV)
		{
			RenLuyen entity = new RenLuyen();
			entity.MaSV = maSV;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_TCD_RenLuyen_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<RenLuyen> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (RenLuyen item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}